package com.example.app1.controller;

import com.example.common.core.Result;
import com.example.common.entity.DemoEntity;
import com.example.feign.app1.App1HelloClient;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;

@RefreshScope
@RestController
@RequestMapping("/hello")
public class DemoController implements App1HelloClient {

    @Value("${test.key:default}")
    private String key;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private CacheManager cacheManager;

    @GetMapping("/get")
    public String get() {
        return "Get";
    }

    @PostMapping("/post")
    public String post() {
        return "Post";
    }

    @GetMapping("/value")
    public String value() {
        return key;
    }

    @GetMapping("/error")
    public Result<Object> error() {
        throw new RuntimeException();
    }

    @GetMapping("/time")
    public Result<Object> time(LocalTime time, LocalDate date, LocalDateTime datetime) {
        System.out.println("time = " + time);
        System.out.println("date = " + date);
        System.out.println("dateTime = " + datetime);
        return Result.success(datetime);
    }

    @GetMapping("/cache")
    public Result<Object> cache() {
        stringRedisTemplate.opsForValue().set("aaa", "111");
        System.out.println(stringRedisTemplate.opsForValue().get("aaa"));
        stringRedisTemplate.opsForValue().set("bbb", "111", 100, TimeUnit.SECONDS);
        System.out.println(stringRedisTemplate.opsForValue().get("bbb"));

        System.out.println(stringRedisTemplate.getKeySerializer().getClass());
        System.out.println(stringRedisTemplate.getValueSerializer().getClass());

        System.out.println(((RedisCacheManager) cacheManager).getCacheConfigurations());

        cacheManager.getCache("bbb").put("111", "222");
        cacheManager.getCache("bbb").put("333", Result.success());
        System.out.println(cacheManager.getCache("bbb").get("111", String.class));
        System.out.println(cacheManager.getCache("bbb").get("333", Result.class));
        return Result.success();
    }


    @Cacheable(value = "cache2")
    @GetMapping("/cache2")
    public Result<Object> cache2() {
        System.out.println("cache2");
        return Result.success();
    }

    @Override
    @GetMapping("/feign1")
    public Result<Object> feign1(LocalDate date) {
        System.out.println("date = " + date);
        return Result.success(date);
    }

    @Override
    @PostMapping("/feign2")
    public Result<DemoEntity> feign2(@RequestBody DemoEntity bean) {
        System.out.println("bean = " + bean);
        return Result.success(bean);
    }

    @Override
    @PostMapping("/feign3")
    public Result<DemoEntity> feign3(String param, @RequestBody DemoEntity bean) {
        System.out.println("param = " + param);
        System.out.println("bean = " + bean);
        return Result.success(bean);
    }
}
